package cn.edu.zjx2.controller;

import cn.edu.mp.entity.Class;
import cn.edu.mp.entity.Grade;
import cn.edu.mp.service.ClassService;
import cn.edu.mp.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/class")
public class ClassController {
    
    @Autowired
    private ClassService classService;
    
    @Autowired
    private GradeService gradeService;
    
    @GetMapping("/page")
    public String page(Model model) {
        List<Class> classes = classService.list();
        List<Grade> grades = gradeService.list();
        
        model.addAttribute("classes", classes);
        model.addAttribute("grades", grades);
        return "class/list";
    }
    
    @PostMapping
    @ResponseBody
    public boolean save(@RequestBody Class clazz) {
        return classService.save(clazz);
    }
    
    @PutMapping
    @ResponseBody
    public boolean update(@RequestBody Class clazz) {
        return classService.updateById(clazz);
    }
    
    @DeleteMapping("/{id}")
    @ResponseBody
    public boolean delete(@PathVariable Long id) {
        return classService.removeById(id);
    }
    
    @GetMapping("/{id}")
    @ResponseBody
    public Class getById(@PathVariable Long id) {
        return classService.getById(id);
    }
} 